﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace SettlersEngine
{
    internal static class Dialogs
    {
        internal enum FileFilter
        {
            Images,
            Videos,
            Music,
        }

        internal static String GetFilterString(FileFilter inFilter)
        {
            switch (inFilter)
            {
                case FileFilter.Images: return "Images Files (*.jpg;*.png;*.bmp;*.gif;*.tif;*.tiff;*.jpeg)|*.jpg;*.png;*.bmp;*.gif;*.tif;*.tiff;*.jpeg|All Files (*.*)|*.*";
                case FileFilter.Videos: return "Video Files (*.m4v;*.avi;*.vob;*.mpg;*.mpeg;*.mp4;*.wmv)|*.m4v;*.avi;*.vob;*.mpg;*.mpeg;*.mp4;*.wmv|All Files (*.*)|*.*";
                case FileFilter.Music: return "Audio Files (*.wav,*.mp3,*.m4a,*.wma,*.mp4)|*.wav,*.mp3,*.m4a,*.wma,*.mp4|All Files (*.*)|*.*";
                default:
                    throw new ArgumentException();
            }
        }

        internal static String OpenSingleFile(
            Window inParent,
            String inTitle, 
            String inLastFileName,
            FileFilter inFilter)
        {
            return OpenSingleFile(inParent, inTitle, inLastFileName, GetFilterString(inFilter));
        }

        internal static String OpenSingleFile(
            Window inParent,
            String inTitle,
            String inLastFileName,
            String inFilter)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.ValidateNames = true;
            dialog.Title = inTitle;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Filter = inFilter;
            dialog.FilterIndex = 0;
            dialog.Multiselect = false;
            dialog.ReadOnlyChecked = true;

            if (!String.IsNullOrWhiteSpace(inLastFileName))
            {
                try
                {
                    dialog.InitialDirectory = System.IO.Path.GetDirectoryName(inLastFileName);
                }
                catch
                {
                }
            }

            bool? result = dialog.ShowDialog(inParent);

            if (result.HasValue && result.Value)
            {
                return dialog.FileName;
            }

            return null;
        }

        internal static String OpenSingleDirectory(
            Window inParent,
            String inTitle,
            String inLastFileName,
            String inFilter)
        {
            Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog();

            dialog.ValidateNames = true;
            dialog.Title = inTitle;
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.Filter = inFilter;
            dialog.FilterIndex = 0;
            dialog.Multiselect = false;
            dialog.ReadOnlyChecked = true;

            if (!String.IsNullOrWhiteSpace(inLastFileName))
            {
                try
                {
                    dialog.InitialDirectory = System.IO.Path.GetDirectoryName(inLastFileName);
                }
                catch
                {
                }
            }

            bool? result = dialog.ShowDialog(inParent);

            if (result.HasValue && result.Value)
            {
                return dialog.FileName;
            }

            return null;
        }
    }
}
